<template>
  <div style="padding: 10px">
    <!--    表格区域-->
    <!--    有啥子功能？注：sortable用于排序  type="success"绿色-->
    <!--      <el-button @click="add" >新增</el-button>-->
    <!--      <el-button>删除</el-button>-->
    <div style="color:green; font-size: 24px;text-align: center;padding: 30px">喜欢的话就赶快剁手吧！</div>
    <div style="margin: 10px 0">
      <!--      搜索区-->
      搜索：
      <el-input v-model="search" placeholder="输入要浏览的商品名称"  style="width: 20%" clearable></el-input>
      <el-button style="margin-left: 20px" @click="load">搜索</el-button>
    </div>

    <el-table :data="tableData"  style="width: 100%">
      <el-table-column prop="id" label="ID" sortable width="180"></el-table-column>
      <el-table-column prop="name" label="名称" width="180"></el-table-column>
      <el-table-column prop="price" label="价格" width="180"></el-table-column>
      <el-table-column  label="状态">
        <template #default="scope">
          <scan v-if="scope.row.state === 1">已支付</scan>
          <scan v-if="scope.row.state != 1">未支付</scan>
        </template>
      </el-table-column>
      <el-table-column label="照片" >
        <template #default="scope">
          <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.photo"
              :preview-src-list="[scope.row.photo]"
              :initial-index="1"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template #default="scope">
          <el-button  size="small" @click="handleEdit(scope.row)">购买</el-button>
          <el-popconfirm title="确认将此商品移出购物车？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="text" size="small">移除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
      <!--      <el-table-column fixed="right" label="Operations" width="120">-->
      <!--        <template #default="scope">-->
      <!--          <el-button type="success" size="small" @click="handleEdit(scope.row)"-->
      <!--          >编辑</el-button-->
      <!--          >-->
      <!--          <el-popconfirm title="确认删除本行信息？" @confirm="handleDelete(scope.row.id)">-->
      <!--            <template #reference>-->
      <!--              <el-button type="text" size="small">删除</el-button>-->
      <!--            </template>-->
      <!--          </el-popconfirm>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <div>
      <el-dialog
          v-model="dialogVisible"
          title="Tips"
          width="30%"
      >
        <el-form :model="form" label-width="120px">
          确认购买？
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false" size="medium">取消</el-button>
        <el-button type="success" @click="save" size="medium">确定</el-button
        >
      </span>
        </template>
      </el-dialog>
    </div>
  </div>

</template>

<script>

import request from "../utils/request";

export default {
  name: 'Cartgoods',
  components: {
  },
  //表中数据
  data() {
    return {
      search:'',
      form:{},
      dialogVisible: false,
      currentPage4:1,
      pageSize:10,
      total1:0,
      tableData: [
      ],
    }
  },
  created() {
    this.load()
  },
//  定义函数：
  methods:{
    filesUploadSuccess (res){
      console.log(res)
      this.form.photo = res.data
    },
    load(){
      request.get("/cartgoods", {
        params:{
          pageNum: this.currentPage4,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res =>{
        console.log(res)
        this.tableData = res.data.records
        this.total1 = res.data.total1
      })
    },
    add(){
      this.dialogVisible = true
      this.form={}
      this.$refs['upload'].clearFiles()
      //  清空表单

    },
    save(){
      this.form.state=1
        request.put("/cartgoods", this.form).then(res => {
          console.log(res)
          if (res.code === '0')
          {
            this.$message(
                {
                  type:"success",
                  message:"新增成功"
                }
            )
          }
          else {
            this.$message(
                {
                  type:"error",
                  message:res.msg
                }
            )
          }
          this.load()
          this.dialogVisible = false
        })
    },
    handleEdit(row)
    {
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
      this.$nextTick(() => {               //处理未来元素，弹窗打开时还不能调用clearFiles（）否则会报错:'clearFiles' of undefined  baidunextTick（）
        this.$refs['upload'].clearFiles() //清空上传的图片
      })
    },
    handleDelete(id) {
      console.log(id)
      request.delete("/cartgoods/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()  // 删除之后重新加载表格的数据
      })
    },
    // handleSizeChange()
    // {
    //
    // },
    // handleCurrentChange()
    // {
    //
    // },

  }
}

</script>